package com.huike.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.pojo.Dept;
import com.huike.service.DeptService;
import com.huike.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dept")
@ResponseBody
public class DeptController {
    @Autowired
    private DeptService deptService;
    @RequestMapping("list")
    public ResultVO getDeptList(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5")  Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Dept> depts = deptService.getDeptList();
        PageInfo pageInfo = new PageInfo(depts,5);

        return new ResultVO(pageInfo);
    }

    @RequestMapping("findDept")
    public ResultVO findDept(){
        List<Dept> dept = deptService.findDept();
        return new ResultVO(dept);
    }
    @RequestMapping("add")
    public ResultVO addDept(Dept dept){

        try{
            deptService.addDept(dept);
            return new ResultVO(200,"添加成功");
        }catch (Exception e){
            return new ResultVO(300,"添加失败");
        }

    }

    @RequestMapping("delete/{id}")
    public ResultVO deleteDept(@PathVariable("id") Integer id){

        try{
            deptService.deleteDept(id);
            return new ResultVO(200,"删除成功");
        }catch (Exception e){
            return new ResultVO(300,"删除失败");
        }

    }

    @RequestMapping("getDeptById")
    public ResultVO getDeptById(Integer id){
            Dept dept =  deptService.getDeptById(id);
            return new ResultVO(dept);
    }

    @RequestMapping("updateDept")
    public ResultVO updateDept(Dept dept){

        try{
            deptService.updateDept(dept);
            return new ResultVO(200,"修改成功");
        }catch (Exception e){
            return new ResultVO(300,"修改失败");
        }

    }
}
